// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <nebula/types/fundamental.h>

namespace nebula {


    /// \brief Base type class for union data
    class TURBO_EXPORT UnionType : public NestedType {
    public:
        static constexpr int8_t kMaxTypeCode = 127;
        static constexpr int kInvalidChildId = -1;

        static turbo::Result<std::shared_ptr<DataType>> create(
                const FieldVector &fields, const std::vector<int8_t> &type_codes,
                UnionMode::type mode = UnionMode::SPARSE) {
            if (mode == UnionMode::SPARSE) {
                return sparse_union(fields, type_codes);
            } else {
                return dense_union(fields, type_codes);
            }
        }

        DataTypeLayout layout() const override;

        std::string to_string(bool show_metadata = false) const override;

        /// The array of logical type ids.
        ///
        /// For example, the first type in the union might be denoted by the id 5
        /// (instead of 0).
        const std::vector<int8_t> &type_codes() const { return type_codes_; }

        /// An array mapping logical type ids to physical child ids.
        const std::vector<int> &child_ids() const { return child_ids_; }

        uint8_t max_type_code() const;

        UnionMode::type mode() const;

    protected:
        UnionType(FieldVector fields, std::vector<int8_t> type_codes, Type::type id);

        static turbo::Status ValidateParameters(const FieldVector &fields,
                                                const std::vector<int8_t> &type_codes,
                                                UnionMode::type mode);

    private:
        std::string compute_fingerprint() const override;

        std::vector<int8_t> type_codes_;
        std::vector<int> child_ids_;
    };

    /// \brief Concrete type class for sparse union data
    ///
    /// A sparse union is a nested type where each logical value is taken from
    /// a single child.  A buffer of 8-bit type ids indicates which child
    /// a given logical value is to be taken from.
    ///
    /// In a sparse union, each child array should have the same length as the
    /// union array, regardless of the actual number of union values that
    /// refer to it.
    ///
    /// Note that, unlike most other types, unions don't have a top-level validity bitmap.
    class TURBO_EXPORT SparseUnionType : public UnionType {
    public:
        static constexpr Type::type type_id = Type::SPARSE_UNION;

        static constexpr const char *type_name() { return "sparse_union"; }

        SparseUnionType(FieldVector fields, std::vector<int8_t> type_codes);

        // A constructor variant that validates input parameters
        static turbo::Result<std::shared_ptr<DataType>> create(FieldVector fields,
                                                               std::vector<int8_t> type_codes);

        std::string name() const override { return "sparse_union"; }
    };

    /// \brief Concrete type class for dense union data
    ///
    /// A dense union is a nested type where each logical value is taken from
    /// a single child, at a specific offset.  A buffer of 8-bit type ids
    /// indicates which child a given logical value is to be taken from,
    /// and a buffer of 32-bit offsets indicates at which physical position
    /// in the given child array the logical value is to be taken from.
    ///
    /// Unlike a sparse union, a dense union allows encoding only the child array
    /// values which are actually referred to by the union array.  This is
    /// counterbalanced by the additional footprint of the offsets buffer, and
    /// the additional indirection cost when looking up values.
    ///
    /// Note that, unlike most other types, unions don't have a top-level validity bitmap.
    class TURBO_EXPORT DenseUnionType : public UnionType {
    public:
        static constexpr Type::type type_id = Type::DENSE_UNION;

        static constexpr const char *type_name() { return "dense_union"; }

        DenseUnionType(FieldVector fields, std::vector<int8_t> type_codes);

        // A constructor variant that validates input parameters
        static turbo::Result<std::shared_ptr<DataType>> create(FieldVector fields,
                                                               std::vector<int8_t> type_codes);

        std::string name() const override { return "dense_union"; }
    };

}  // namespace nebula
